package xmls;

import Levels.ChapterId;
import Levels.Complexity;
import Questions.Question;
import Questions.QuestionsBook;
import Questions.QuestionsFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 21.11.13
 */
public class SaxParserQuestions extends DefaultHandler {
    private final String TG_QUESTIONS="questions";
    private final String ATR_CHAPTER="chapter";
    private final String TG_QUESTION="question";
    private final String ATR_VALUE_QUEST="value";
    private final String ATR_TYPE_QUEST="type";
    private final String TG_ANSWER="answer";
    private final String ATR_RIGHT_ANSW="right";
    private final String TG_EASY="easy";
    private final String TG_MIDDLE="middle";
    private final String FIRST_CHAPTER_NAME = "first";
    private final String SECOND_CHAPTER_NAME = "second";
    private QuestionsBook questionsBook;
    private String currentTaq="";
    private Attributes currentAtts;
    private QuestionsFactory questionsFactory;
    // data for questions
    private ChapterId chapterId;
    private Complexity complexity;
    private ArrayList<String> varList;
    private String question;
    private String rightAnswer;
    private boolean hasVariants = false;

    public SaxParserQuestions(){
        this.questionsBook = new QuestionsBook();
        this.questionsFactory = new QuestionsFactory();
        this.varList = new ArrayList<>();
        this.question = "";
        this.rightAnswer = "";

    }


    public QuestionsBook getQuestionsBook() {
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        try {
            SAXParser standartParser = parserFactory.newSAXParser();
            SaxParserQuestions parserQuestions = new SaxParserQuestions();
            try {
                standartParser.parse(new File("E:\\PROGRAMMING\\IDEA_WORK\\Inessa_Igor\\src\\xmls\\Questions"),parserQuestions);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (ParserConfigurationException | SAXException e) {
          e.printStackTrace();
        }

        return questionsBook;
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("Questions parsing finished...");
    }

    @Override
    public void startDocument() throws SAXException {
        System.out.println("Start parsing questions doc...");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
       this.currentTaq=qName;
       this.currentAtts=attributes;

     }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(qName.equals(TG_QUESTION)){
            Question addingQuestion = questionsFactory.getQuestion(question,rightAnswer,varList);
            questionsBook.addQuestionToChapter(chapterId,complexity,addingQuestion);
            question = "";
            rightAnswer = "";
            varList.clear();
            hasVariants=false;


        }

        currentTaq="";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(currentTaq.equals(TG_QUESTIONS)){
           if(currentAtts.getValue(ATR_CHAPTER).equals(FIRST_CHAPTER_NAME)){
                chapterId = ChapterId.CHAPTER_ONE;
            } else if(currentAtts.getValue(ATR_CHAPTER).equals(SECOND_CHAPTER_NAME)){
               chapterId = ChapterId.CHAPTER_TWO;
           }
        }

        if(currentTaq.equals(TG_EASY)){
            complexity=Complexity.EASY;
        }

        if(currentTaq.equals(TG_MIDDLE)){
            complexity=Complexity.MEDIUM;
        }

        if(currentTaq.equals(TG_QUESTION)){
            question = currentAtts.getValue(ATR_VALUE_QUEST);
            if(currentAtts.getValue(ATR_TYPE_QUEST).equals("many")){
                hasVariants = true;
            }
        }

        if(currentTaq.equals(TG_ANSWER)){
            String answer = new String(ch,start,length);
            if(currentAtts.getValue(ATR_RIGHT_ANSW).equals("true")){
                rightAnswer = answer;
            }
            if(hasVariants){
                varList.add(answer);
            }
        }
    }
}
